package com.hslxy.niukewang.service;

import com.hslxy.niukewang.dao.AlphaDao;
import com.hslxy.niukewang.dao.DiscussPostMapper;
import com.hslxy.niukewang.dao.UserMapper;
import com.hslxy.niukewang.entity.DiscussPost;
import com.hslxy.niukewang.entity.User;
import com.hslxy.niukewang.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Date;

@Service
//设置这个类是不是单例的
//@Scope("prototype")
public class AlphaDaoService {

    @Autowired
    private AlphaDao alphaDao;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DiscussPostMapper discussPostMapper;

//    用于事务管理
    @Autowired
    private TransactionTemplate transactionTemplate;

    public AlphaDaoService() {
        System.out.println("实例化");
    }
//    构造器之后启动
    @PostConstruct
    public void init(){
        System.out.println("初始化init");
    }

    @PreDestroy
    public void destory(){
        System.out.println("销毁");
    }

    public String find(){
        return alphaDao.select();
    }

//    REQUIRED: 外部事物调用我，如果调用者不存在事务则用我的事务
//    REQUIRED_NEW: 外部事物调用我，如果调用者存在事务,我也不管还用我的事务
//    NESTED: 嵌套事务
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public Object save1(){
//        新增用户
        User user = new User();
        user.setUsername("kunkun");
        user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
        user.setPassword(CommunityUtil.md5("123" + user.getSalt()));
        user.setEmail("aa@qq.com");
        user.setHeaderUrl("http://images.nowcoder.com/head/100t.png");
        user.setCreateTime(new Date());
        userMapper.insertUser(user);
//        新增帖子
        DiscussPost discussPost = new DiscussPost();
        discussPost.setTitle("1");
        discussPost.setUserId(user.getId());
        discussPost.setContent("11111111111111111111");
        discussPost.setCreateTime(new Date());
        discussPostMapper.insertDiscussPost(discussPost);

        Integer.valueOf("abc");
        return "ok";
    }


    public Object save2(){
        transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        return transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus status) {
                User user = new User();
                user.setUsername("kunkun");
                user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
                user.setPassword(CommunityUtil.md5("123" + user.getSalt()));
                user.setEmail("aa@qq.com");
                user.setHeaderUrl("http://images.nowcoder.com/head/100t.png");
                user.setCreateTime(new Date());
                userMapper.insertUser(user);
                //        新增帖子
                DiscussPost discussPost = new DiscussPost();
                discussPost.setTitle("1");
                discussPost.setUserId(user.getId());
                discussPost.setContent("11111111111111111111");
                discussPost.setCreateTime(new Date());
                discussPostMapper.insertDiscussPost(discussPost);

                Integer.valueOf("abc");
                return "ok";
            }
        });
    }
}
